package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowInfoStatusEnum;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.mapper.BorrowInfoMapper;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.BorrowInfoApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.service.BorrowInfoService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.LendService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 借款信息表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements BorrowInfoService {


    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    DictService dictService;
    @Resource
    BorrowerService borrowerService;
    @Resource
    LendService lendService;
 
    @Override
    public Integer getBorrowInfoStatusByUserId(Long userId) {
        QueryWrapper<BorrowInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        BorrowInfo borrowInfo = baseMapper.selectOne(queryWrapper);
        if (borrowInfo == null){
            return 0;
        }
        return borrowInfo.getStatus();
    }

    @Override
    public void saveBorrowerInfo(BorrowInfo borrowInfo, Long userId) {
        borrowInfo.setUserId(userId);
        borrowInfo.setBorrowYearRate(borrowInfo.getBorrowYearRate().divide(new BigDecimal("100")));
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_RUN.getStatus());
        baseMapper.insert(borrowInfo);
    }

    @Override
    public List<BorrowInfo> getList() {
        List<BorrowInfo> borrowInfos = baseMapper.selectList(null);
        for (BorrowInfo borrowInfo : borrowInfos) {
            Long userId = borrowInfo.getUserId();
            UserInfo userInfo = userInfoMapper.selectById(userId);
            borrowInfo.getParam().put("name",userInfo.getName());
            borrowInfo.getParam().put("mobile",userInfo.getMobile());
            borrowInfo.getParam().put("returnMethod",
                    dictService.getDictNameByDictCodeAndValueId("returnMethod",borrowInfo.getReturnMethod()));
            borrowInfo.getParam().put("moneyUse",
                    dictService.getDictNameByDictCodeAndValueId("moneyUse",borrowInfo.getMoneyUse()));
            borrowInfo.getParam().put("status",BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus()));

        }
        return borrowInfos;
    }

    @Override
    public Map<String, Object> showBorrowInfo(Long id) {
        Map<String,Object> map = new HashMap<>();

        //借款详情 borrowInfo
        BorrowInfo borrowInfo = baseMapper.selectById(id);
        Long userId = borrowInfo.getUserId();
        UserInfo userInfo = userInfoMapper.selectById(userId);
/*        borrowInfo.getParam().put("name",userInfo.getName());
        borrowInfo.getParam().put("mobile",userInfo.getMobile());*/
        borrowInfo.getParam().put("returnMethod",
                dictService.getDictNameByDictCodeAndValueId("returnMethod",borrowInfo.getReturnMethod()));
        borrowInfo.getParam().put("moneyUse",
                dictService.getDictNameByDictCodeAndValueId("moneyUse",borrowInfo.getMoneyUse()));
        borrowInfo.getParam().put("status",BorrowInfoStatusEnum.getMsgByStatus(borrowInfo.getStatus()));

        //借款人详情 Borrower
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        borrowerQueryWrapper.eq("user_id",userId);
        Borrower one = borrowerService.getOne(borrowerQueryWrapper);

        BorrowerDetailVO borrowerDetailVO = borrowerService.show(one.getId());
        map.put("borrower",borrowerDetailVO);
        map.put("borrowInfo",borrowInfo);
        return map;
    }

    @Override
    public void approvalSubmit(BorrowInfoApprovalVO borrowInfoApprovalVO) {
        //修改borrow_info 表中status字段
        Long id = borrowInfoApprovalVO.getId();
        BorrowInfo borrowInfo = baseMapper.selectById(id);
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_OK.getStatus());
        baseMapper.updateById(borrowInfo);

        //调用标的业务系统生成新标的 存入lend表
        if (borrowInfoApprovalVO.getStatus().intValue() ==
                BorrowInfoStatusEnum.CHECK_OK.getStatus().intValue()){
            lendService.createLend(borrowInfo,borrowInfoApprovalVO);
        }
    }
}

